package com.moon.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moon.common.model.entity.InterfaceInfo;
import com.moon.common.model.entity.UserInterfaceInfo;
import com.moon.common.model.vo.InterfaceInfoVO;
import com.moon.project.annotation.AuthCheck;
import com.moon.project.common.BaseResponse;
import com.moon.project.common.ErrorCode;
import com.moon.project.common.ResultUtils;
import com.moon.project.exception.BusinessException;
import com.moon.project.mapper.UserInterfaceInfoMapper;
import com.moon.project.service.InterfaceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {

    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    @Resource
    private InterfaceInfoService interfaceInfoService;

    /**
     * 获取调用次数前n的接口
     *
     * @param count
     * @return
     */
    @GetMapping("/invokeInterface/top")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfoCount(Integer count) {
        //查询调用次数最多的接口信息列表
        List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoMapper.ListTopInterfaceInfo(count);
        //将接口信息按照接口接口ID分组，便于关联查询
        Map<Long, List<UserInterfaceInfo>> interfaceInfoIdObjMap = userInterfaceInfoList.stream()
                .collect(Collectors.groupingBy(UserInterfaceInfo::getInterfaceInfoId));
        //查询接口信息的条件包装器
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        //设置查询条件。使用接口信息ID在接口信息映射中的键集合进行条件匹配
        queryWrapper.in("id", interfaceInfoIdObjMap.keySet());
        //调用接口信息的list方法。传入包装器，获取符合条件的接口信息列表
        List<InterfaceInfo> list = interfaceInfoService.list(queryWrapper);
        //判断查询结果是否为空
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        //构建接口信息VO表，使用流式处理将接口信息映射为接口信息VO对象，并加入列表中
        List<InterfaceInfoVO> interfaceInfoVOList = list.stream().map(interfaceInfo -> {
            //创建一个新的接口信息VO对象
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            //将接口信息复制到对象VO中
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            //从接口信息ID对应的映射中获取调用次数
            int totalNum = interfaceInfoIdObjMap.get(interfaceInfo.getId()).get(0).getTotalNum();
            //将调用次数设置到接口信息VO对象中
            interfaceInfoVO.setTotalNum(totalNum);
            return interfaceInfoVO;
        }).collect(Collectors.toList());

        return ResultUtils.success(interfaceInfoVOList);
    }


    /**
     * 获取调用次数前n的接口
     *
     * @param count
     * @return
     */
    //@GetMapping("/invokeInterfaceInfo/top")
    //@AuthCheck(mustRole = "admin")
    //public BaseResponse<List<InterfaceInfoVO>> listTopInterfaceInfo(Integer count) {
    //
    //    return null;
    //}

}
